package gui;

import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import Entity.FlowData;
import config.ColorConfig;
import config.TunnelPanelConfig;

@SuppressWarnings("serial")
public class TunnelPanel extends JPanel{

	public Map<String,ItemPanel> tunnels;
	public Integer count = 0;
	private int gidLength = 2;
	
	//config
	private TunnelPanelConfig config;
	
	TunnelPanel(){
		super();
		config = new TunnelPanelConfig();
		GUIManager.getConfig(config);
		tunnels = new HashMap<String, ItemPanel>();
		//layout
	}

	public void start() {	
		
		//layout
		setBorder(BorderFactory.createLineBorder(ColorConfig.getColor("tunnelPanelColor")));
		setFocusable(true);	
		setLayout(new GridLayout(count, gidLength));
		
		//add
		for(ItemPanel value : tunnels.values()) {
			this.add(value);
		}
		updateUI();
	}
	
	public void addTunnel(String name,FlowData flowData) {
		if(!tunnels.containsKey(name) && count < config.getMaxCount()) {
			removeAll();
			ItemPanel item = new ItemPanel(name,flowData,this);
			item.start();
			tunnels.put(name,item);
			count++;
			start();
		}
	}
	
	public void delTunnel(String name) {
		if(tunnels.containsKey(name)) {
			removeAll();
			tunnels.remove(name);
			count--;
			start();
		}
	}

	public TunnelPanelConfig getConfig() {
		// TODO Auto-generated method stub
		return config;
	}
	

	public void updatePanel() {
		// TODO Auto-generated method stub
		for(ItemPanel t:tunnels.values()) {
			t.updateItem();
		}
	}	
}
